package com.zmytest.service.impl;

import com.zmytest.dto.ResponseDto;
import com.zmytest.dto.RoleDto;
import com.zmytest.model.Permission;
import com.zmytest.model.Role;
import com.zmytest.repository.PermissionRepository;
import com.zmytest.repository.RoleRepository;
import com.zmytest.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.LinkedList;
import java.util.List;

/**
 * Created on 2018/12/20.
 *
 * @author zhangmingyu
 */
@Service
@Slf4j
public class RoleServiceImpl implements RoleService {

    private final PermissionRepository permissionRepository;
    private final RoleRepository roleRepository;

    @Autowired
    public RoleServiceImpl(PermissionRepository permissionRepository, RoleRepository roleRepository) {
        this.permissionRepository = permissionRepository;
        this.roleRepository = roleRepository;
    }

    @Override
    public ResponseDto<Role> create(RoleDto roleDto) {
        if (!ObjectUtils.isEmpty(roleDto.getId())) {
            return ResponseDto.fail("user id must be null");
        }
        ResponseDto<List<Permission>> getPermissionListResult = getPermissionList(roleDto);
        if (!getPermissionListResult.isSuccess()) {
            return ResponseDto.fail(getPermissionListResult.getMessage());
        }
        Role role = new Role(roleDto.getName(),getPermissionListResult.getData());
        Role result = roleRepository.save(role);
        return ResponseDto.success(result);
    }

    @Override
    public ResponseDto<Role> find(Long id) {
        Role result = roleRepository.findOne(id);
        return ResponseDto.success(result);
    }

    @Override
    public ResponseDto<Role> update(RoleDto roleDto) {
        Long id = roleDto.getId();
        if (ObjectUtils.isEmpty(id)) {
            return ResponseDto.fail("role id can not be null");
        }
        Role role = roleRepository.findOne(id);
        if (ObjectUtils.isEmpty(role)) {
            return ResponseDto.fail("can not find role by id["+id+"]");
        }
        ResponseDto<List<Permission>> getPermissionListResult = getPermissionList(roleDto);
        if (!getPermissionListResult.isSuccess()) {
            return ResponseDto.fail(getPermissionListResult.getMessage());
        }
        role.update(roleDto,getPermissionListResult.getData());
        Role result = roleRepository.save(role);
        return ResponseDto.success(result);
    }

    @Override
    public ResponseDto<String> delete(Long id) {
        roleRepository.delete(id);
        return ResponseDto.success("delete success");
    }

    private ResponseDto<List<Permission>> getPermissionList(RoleDto roleDto){
        List<Permission> permissionList = new LinkedList<>();
        for (Long permissionId : roleDto.getPermissionId()) {
            Permission permission = permissionRepository.getOne(permissionId);
            if (ObjectUtils.isEmpty(permission)) {
                return ResponseDto.fail("can not find Permission by id ["+permissionId+"]");
            }
            permissionList.add(permission);
        }
        return ResponseDto.success(permissionList);
    }
}
